//https://leetcode.cn/problems/sort-array-by-parity-ii/
//解法1:自己求解；
//解法2:官方求解；
//解法2:双指针法；

class Solution {
public:
    vector<int> sortArrayByParityII(vector<int>& nums) {
        if(nums.empty()||nums.size() & 0x01 == 1) return {};
        std::vector<int> odd;
        std::vector<int> event;
        for(size_t i=0;i<nums.size();++i){
            if(nums[i] &0x01==1){
                odd.push_back(nums[i]);
            }else{
                event.push_back(nums[i]);
            }
        }
        std::vector<int> res;
        for(size_t i=0;i<odd.size();++i){
            res.push_back(event[i]);
            res.push_back(odd[i]);
        }
        return res;
    }
};


class Solution2 {
public:
    vector<int> sortArrayByParityII(vector<int>& nums) {
        int n = nums.size();
        vector<int> ans(n);

        int i = 0;
        for (int x: nums) {
            if (x % 2 == 0) {
                ans[i] = x;
                i += 2;
            }
        }
        i = 1;
        for (int x: nums) {
            if (x % 2 == 1) {
                ans[i] = x;
                i += 2;
            }
        }
        return ans;
    }
};


class Solution3 {
public:
    vector<int> sortArrayByParityII(vector<int>& nums) {
        int n = nums.size();
        int j = 1;
        for (int i = 0; i < n; i += 2) {
            if (nums[i] % 2 == 1) {
                while (nums[j] % 2 == 1) {
                    j += 2;
                }
                swap(nums[i], nums[j]);
            }
        }   
        return nums;
    }
};